<?php

namespace app\api\utils;

use app\api\utils\Lotus;
/**
 * lotus api
 */
class LotusApi  {


    
    public function ChainGetGenesis(){
        return Lotus::to_request('Filecoin.ChainGetGenesis',[]);
    }

    public function ChainGetBlock($cid){
        return Lotus::to_request('Filecoin.ChainGetBlock',[['/'=>$cid]]);
    }

    public function ChainGetBlockMessages($cid){
        return Lotus::to_request('Filecoin.ChainGetBlockMessages',[['/'=>$cid]]);
    }


    public function ChainGetMessage($cid){
        return Lotus::to_request('Filecoin.ChainGetMessage',[['/'=>$cid]]);
    }


    public function ChainGetParentMessages($cid){
        return Lotus::to_request('Filecoin.ChainGetParentMessages',[['/'=>$cid]]);
    }
    
    public function ChainGetParentReceipts($cid){
        return Lotus::to_request('Filecoin.ChainGetParentReceipts',[['/'=>$cid]]);
    }

    public function StateListMessages($cid){
        return Lotus::to_request('Filecoin.StateListMessages',[['/'=>$cid]]);
    }
    
    /**
     * from    
     * to
     */
    public function ChainGetPath($cid){
        return Lotus::to_request('Filecoin.ChainGetPath',[[['/'=>$cid],['/'=>$cid]],[['/'=>$cid],['/'=>$cid]]]);
    }
    

    public function ChainGetTipSetByHeight($height){
        return Lotus::to_request('Filecoin.ChainGetTipSetByHeight',[(int)$height,null]);
    }

    public function ChainHead(){
        return Lotus::to_request('Filecoin.ChainHead',[]);
    }

    
    public function ChainHasObj($cid){
        return Lotus::to_request('Filecoin.ChainHasObj',[['/'=>$cid]]);
    }

    public function ChainReadObj($cid){
        return Lotus::to_request('Filecoin.ChainReadObj',[['/'=>$cid]]);
    }



    public function GasEstimateGasPremium(){
        return Lotus::to_request('Filecoin.GasEstimateGasPremium',[42, null, 9, null]);
        //{"jsonrpc":"2.0","result":"99606","id":1}
    }
    
    public function NetBlockList(){
        return Lotus::to_request('Filecoin.NetBlockList',[null]);
        //{"jsonrpc":"2.0","result":"99606","id":1}
    }


    public function StateListMiners(){
        return Lotus::to_request('Filecoin.StateListMiners',[null]);
        //{"jsonrpc":"2.0","result":["t01017","t01015","t01000","t01011","t01010","t01008","t01003","t01019","t01004","t01007","t01013"],"id":1}
    }

    public function StateMinerPower($address){
        return Lotus::to_request('Filecoin.StateMinerPower',[$address,null]);
    }

    public function StateMarketBalance($address){
        return Lotus::to_request('Filecoin.StateMarketBalance',[$address,null]);
    }

    public function StateMinerInfo($address){
        return Lotus::to_request('Filecoin.StateMinerInfo',[$address,null]);
    }

    public function StateMinerPreCommitDepositForPower($address){
        return Lotus::to_request('Filecoin.StateMinerPreCommitDepositForPower',[$address,null,null]);
    }

    public function StateMinerInitialPledgeCollateral($address){
        return Lotus::to_request('Filecoin.StateMinerInitialPledgeCollateral',[$address,null,null]);
    }

    public function StateMinerAvailableBalance($address){
        return Lotus::to_request('Filecoin.StateMinerAvailableBalance',[$address,null]);
    }


    public function MinerGetBaseInfo($address){
        $last = (new \addons\explore\model\Block)->field('height,cid')->order('height desc')->find();
        // $address = $this->_get('address');
        // $height = $this->_get('height');
        // $cid = $this->_get('cid');
        // return Lotus::to_request('Filecoin.MinerGetBaseInfo',[$address]);
        return Lotus::to_request('Filecoin.MinerGetBaseInfo',[$address,(int)$last['height'],[['/'=>$last['cid']]]]);
        //{"jsonrpc":"2.0","result":{"MinerPower":"241055039488","NetworkPower":"417148698624","Sectors":[{"SealProof":7,"SectorNumber":193,"SealedCID":{"/":"bagboea4b5abcaxdgkwdsm6bp7z26gvkj7y4gmf4uqse57dflcyymadzizje6qlbf"}}],"WorkerKey":"t3w4e6xgrdwwwudwfkonhrgxc4pwih4vd3lc3kxzdldeq7agyjvazxespyhdgdug7o6bgm3ubdkqo3t3ysjyia","SectorSize":536870912,"PrevBeaconEntry":{"Round":898104,"Data":"pMnO+Fgxi5BWDrA3PT0pwbsh7MYol+3te+9cfCcQ2//3JqOJl7P9SSDa5BOvwWm+BgQfRHkdmHsnmyjQMFLs5V3Xq3887D25SRKMemEZ6vqG26aE7cZSaCud/Qu05YQE"},"BeaconEntries":null,"EligibleForMining":true},"id":1}
    }

    //转账
    public function MpoolPushMessage($from,$to,$value,$GasLimit){
        return Lotus::to_request('Filecoin.MpoolPushMessage',[[
                    'to'=>$to,
                    'from'=>$from,
                    'value'=>$value,
                    'GasLimit'=>$GasLimit,
                    'GasFeeCap'=>'0',
                    'GasPremium'=>'0',
                    'Method'=>0,
                    'Params'=>'Ynl0ZSBiYXNl',
                ],['MaxFee'=>"0"]]);
    }




    public function StateVMCirculatingSupplyInternal(){
        return Lotus::to_request('Filecoin.StateVMCirculatingSupplyInternal',[null]);
        //{"jsonrpc":"2.0","result":{"FilVested":"15751395219027985717903048","FilMined":"50691054095390716177445","FilBurnt":"57666107847689717582712","FilLocked":"4218762923847849477263","FilCirculating":"0","FilReserveDisbursed":"-599500000000000000000000000"},"id":1}
    }

    public function StateVMCirculatingSupplyInternalCid($cid){
        return Lotus::to_request('Filecoin.StateVMCirculatingSupplyInternal',[[['/'=>$cid],['/'=>$cid]]]);
        //{"jsonrpc":"2.0","result":{"FilVested":"15751395219027985717903048","FilMined":"50691054095390716177445","FilBurnt":"57666107847689717582712","FilLocked":"4218762923847849477263","FilCirculating":"0","FilReserveDisbursed":"-599500000000000000000000000"},"id":1}
    }


    public function WalletBalance($address){
        return Lotus::to_request('Filecoin.WalletBalance',[$address]);
        //{"jsonrpc":"2.0","result":"14736751952589489461189","id":1}
    }

    
    public function WalletValidateAddress($address){
        return Lotus::to_request('Filecoin.WalletValidateAddress',[$address]);
        //{"jsonrpc":"2.0","result":"14736751952589489461189","id":1}
    }

    public function WalletExport($address){
        return Lotus::to_request('Filecoin.WalletExport',[$address]);
        //{"jsonrpc":"2.0","result":{"Type":"bls","PrivateKey":"k2k6YY5mBHZpa1NfOxe3Q8sHmPa7JIAR1kYfOoTRMg4="},"id":1}
    }


    public function WalletImport(){
        $private_key = $this->_get('private_key');
        return Lotus::to_request('Filecoin.WalletImport',["Type"=>"secp256k1","PrivateKey"=>$private_key]);
        //{"jsonrpc":"2.0","result":{"Type":"bls","PrivateKey":"k2k6YY5mBHZpa1NfOxe3Q8sHmPa7JIAR1kYfOoTRMg4="},"id":1}
    }


    public function WalletNew(){
        return Lotus::to_request('Filecoin.WalletNew',['secp256k1']);
        //{"jsonrpc":"2.0","result":"t3rhqnxkr4pc3d2avb6lfrtfj6hg6lzr2bjhzl7bvjpfrq62vacqxrpvvaeduggiimjpeqn7hvc4vaewanpxhq","id":1}
    }

    
    public function ClientImport(){
        $path_1 = $this->_get('path_1');
        $iscar = $this->_get('iscar')==0?false:true;
        return Lotus::to_request('Filecoin.ClientImport',[['IsCAR'=>$iscar,'Path'=>$path_1]]);
        //{"jsonrpc":"2.0","result":{"Root":{"/":"bafykbzacedyxkcv3klubmon2dcbu7bnp4hr5mf4hdgrhvscdxuitfa2zivv5c"},"ImportID":11},"id":1}
    }


    public function ClientGenCar(){
        $path_1 = $this->_get('path_1');
        $path_2 = $this->_get('path_2');
        $iscar = $this->_get('iscar')==0?false:true;
        return Lotus::to_request('Filecoin.ClientGenCar',[['Path'=>$path_1,'IsCAR'=>$iscar],$path_2]);
        //
    }


    public function ClientStartDeal(){
        $TransferType = $this->_get('TransferType');
        $cid = $this->_get('cid');
        $PieceSize = $this->_get('PieceSize');
        $Wallet = $this->_get('Wallet');
        $Miner = $this->_get('Miner');
        $EpochPrice = $this->_get('EpochPrice');
        $MinBlocksDuration = $this->_get('MinBlocksDuration');
        $ProviderCollateral = $this->_get('ProviderCollateral');
        $DealStartEpoch = $this->_get('DealStartEpoch');
        // return Lotus::to_request('Filecoin.ChainHead',[]);
        // if($res['success']){
        //     // dump($res);
        //     $head_info = json_decode($res['message'],1);
        //     $DealStartEpoch = $head_info['result']['Height'];
        // }
        return Lotus::to_request('Filecoin.ClientStartDeal',[(object)[
            'Data'=>[
                'TransferType'=>$TransferType,
                'Root'=>(object)['/'=>$cid],
                'PieceCid'=>null,
                'PieceSize'=>(int)$PieceSize,
            ],
            'Wallet'=>$Wallet,
            'Miner'=>$Miner,
            'EpochPrice'=>$EpochPrice,
            'MinBlocksDuration'=>(int)$MinBlocksDuration,
            'ProviderCollateral'=>$ProviderCollateral,
            'DealStartEpoch'=>(int)$DealStartEpoch,
            'FastRetrieval'=>true,
            'VerifiedDeal'=>false,
        ]]);
        // message" => "{"jsonrpc":"2.0","result":{"/":"bafyreihwucycckvf4yno5rpw6hjlqlpdiar5zuwp5zwkh5b4txld5kko24"},"id":1}
    }

    


    /**
     * 查看列表
     State 26 -失败  23-待上链  7-成功
     */
    public function ClientListDeals(){
        return Lotus::to_request('Filecoin.ClientListDeals',[]);
        /**
        "jsonrpc" => "2.0"
        "result" => array:6 [▼
            0 => array:15 [▼
                "ProposalCid" => array:1 [▶]
                "State" => 26
                "Message" => "unexpected deal status while waiting for data request: 11 (StorageDealFailing). Provider message: deal rejected: deal start epoch has already elapsed"
                "DealStages" => null
                "Provider" => "w01006"
                "DataRef" => array:5 [▼
                "TransferType" => "graphsync"
                "Root" => array:1 [▼
                "/" => "bafykbzacedyxkcv3klubmon2dcbu7bnp4hr5mf4hdgrhvscdxuitfa2zivv5c"
                ]
                "PieceCid" => null
                "PieceSize" => 1048576
                "RawBlockSize" => 0
                ]
                "PieceCID" => array:1 [▼
                    "/" => "baga6ea4seaqep4ewrocavuy5aq7udd322a6liia7p5sfngofze2uj73drojm6fa"
                ]
                "Size" => 2080768
                "PricePerEpoch" => "0"
                "Duration" => 4041
                "DealID" => 0
                "CreationTime" => "2021-06-04T10:51:39.032294568+08:00"
                "Verified" => true
                "TransferChannelID" => null
                "DataTransfer" => null
            ]
        * 
        */   
    }



    /**
     * 获取流通量
     */
    public function StateCirculatingSupply($from_cid,$to_cid){
        return Lotus::to_request('Filecoin.StateCirculatingSupply',[['/'=>$from_cid],['/'=>$to_cid]]);

    }


}
